<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      ref="form"
      inline
      label-width="140px"
      :model="formData"
      :rules="formRules"
    >
      <!-- 下拉框 -->
      <el-form-item label="归属学科" prop="subject_id">
        <!-- <el-select v-model="formData.type" placeholder="请选择" @change="changeType"> -->
        <el-select v-model="formData.subject_id" clearable placeholder="请选择" @change="changeSubject">
          <el-option
            v-for="item in treeData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="归属班级" prop="class_id">
        <el-select v-model="formData.class_id" clearable placeholder="请确保已选择学科">
          <el-option
            v-for="item in classList"
            :key="item.id"

            :label="item.class_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学生姓名" prop="student_name">
        <el-input v-model="formData.student_name" placeholder="请输入学生姓名" />
      </el-form-item>
      <el-form-item label="手机号码" prop="student_phone">
        <el-input v-model="formData.student_phone" placeholder="请输入手机号码" />
      </el-form-item>
      <el-form-item label="密码" prop="student_password">
        <el-input v-model="formData.student_password" placeholder="默认密码是 姓名+admin88" />
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="formData.student_sex">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="formData.student_state">
          <el-radio :label="1">待审核</el-radio>
          <el-radio :label="2">正常</el-radio>
          <el-radio :label="3">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-row type="flex" justify="end">
        <el-button
          type="primary"
          size="small"
          @click="onClose"
        >取消</el-button>
        <el-button size="small" @click="onSubmit">确定</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { getSubjectClass, addStudent, updateStudent } from '@/api/studenets'

export default {
  name: 'AddFormLink',
  props: {
    treeData: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    return {
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        subject_id: '',
        class_id: '',
        student_name: '',
        student_password: '',
        student_phone: '',
        student_sex: 1,
        student_state: 1
      },
      classList: [],
      // 表单验证规则
      formRules: {
        subject_id: [{ required: true, message: '请输入', trigger: 'change' }],
        class_id: [{ required: true, message: '请输入', trigger: 'change' }],
        student_name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        student_phone: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }

    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  created() {
  },
  methods: {
    // 选择学科
    async changeSubject(event) {
      console.log(event)
      const res = await getSubjectClass({
        subject_id: event,
        page: 1,
        limit: 1000
      })
      // console.log(res)
      this.classList = res.list
    },
    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.formData.id
        ? await updateStudent(this.formData)
        : await addStudent(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.onClose()
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        subject_id: '',
        class_id: '',
        student_name: '',
        student_password: '',
        student_phone: '',
        student_sex: 1,
        student_state: 1
      }
      // 移除校验
      this.$refs.form.clearValidate()
      this.formRules.subject_id = []
      this.formRules.class_id = []
      setTimeout(() => {
        this.formRules.subject_id = [{ required: true, message: '请输入', trigger: 'change' }]
        this.formRules.class_id = [{ required: true, message: '请输入', trigger: 'change' }]
      }, 0)
      this.dialogShow = false
    }
  }
}
</script>
<style scoped></style>
